# gml graph language lexer+parser
%{
#include <stdio.h>
int nlines= 1;
%}

start	= - head2? 'graph' - OPEN gmldata CLOSE

gmldata	= (pair2 | id OPEN gmldata CLOSE)*

id	= ID { }

# before graph are often 2 lines with version/creator
head2 = (ID STRING | ID ID | ID NUMBER)+

pair2 = (id value) { printf("pair at line %d\n",nlines); }

value	= ID { }
	| MFPNUM { }
	| FPNUM { }
	| MNUMBER { }
	| NUMBER { }
	| STRING


ID	= < [a-zA-Z_][a-zA-Z_0-9]* > -	{ printf("ID=%s\n",yytext); }
STRING	= '"' < [^\"]* > '"' -	{  printf("STRING=%s\n",yytext);  }
MFPNUM	= '-' < [0-9.]+[0-9]?  >  	-	{ printf("MINFP=%s\n",yytext); }
FPNUM	= < [0-9.]+[0-9]?  >  	-	{ printf("FP=%s\n",yytext); }
MNUMBER	= '-' < [0-9]+ >	-	{ printf("MINNUM=%s\n",yytext); }
NUMBER	= < [0-9]+ >	-	{  printf("NUM=%s\n",yytext); }

OPEN	= '['		-
CLOSE	= ']'		-

-=		(space | comment)*
space=		' ' | '\t' | end-of-line
comment=	'#' (!end-of-line .)* end-of-line
end-of-line=	( '\r\n' | '\n' | '\r' | '\n\r' )		{ nlines++; }
end-of-file=	!.

%%

int main()
{
  int status = 1;
  char *str=NULL;
  while (!feof(stdin)) {
    status=yyparse();
    if (status == 0) {
	break;
    }
  }
  if (status == 0) {
    str="syntax error";
  } else {
    str="syntax oke";
  }
  printf("%d lines status=%d %s\n", nlines,status,str);
  return 0;
}
